package creditcard.mvc;

import framework.account.IAccount;
import framework.account.IEntry;
import framework.command.DebtCommand;

import java.awt.*;
import java.text.NumberFormat;
import java.util.Date;

public class MonthlyBillReportDialog extends javax.swing.JDialog {


    public MonthlyBillReportDialog(Frame parent, IAccount account) {
        super(parent);
        setTitle("Monthly Billing Report");

        getContentPane().setLayout(null);
        setSize(405, 367);
        setVisible(false);
        getContentPane().add(JScrollPane1);
        JScrollPane1.setBounds(24, 24, 358, 240);
        JScrollPane1.getViewport().add(JTextArea1);
        JTextArea1.setBounds(0, 0, 355, 237);
        JButton_OK.setText("OK");
        JButton_OK.setActionCommand("OK");
        getContentPane().add(JButton_OK);
        JButton_OK.setBounds(156, 276, 96, 24);
        Font font = new Font("Verdana", Font.BOLD, 14);
        JTextArea1.setFont(font);

        StringBuilder builder = new StringBuilder();
        double credit = 0d, charge = 0d, prevBal = account.getPrevBalance(), newBal = 0d, total = 0d;
        int thisMonth = new Date().getMonth();
        if (account.getTransactionHistory() != null)
            for (IEntry entry : account.getTransactionHistory()) {
                if (entry.getDate().getMonth() == thisMonth) {
                    if (entry.getType().equals(DebtCommand.type)) {
                        charge += entry.getAmount();
                    } else {
                        credit += entry.getAmount();
                    }
                }

            }
        newBal = prevBal - credit + charge * account.getInterestRate() * (prevBal - credit);
        account.setPrevBalance(newBal);
        NumberFormat format = NumberFormat.getCurrencyInstance();
        builder.append("Monthly Bill");
        builder.append("\nPrevious month balance :" + format.format(prevBal));
        builder.append("\nTotal charges :" + format.format(charge));
        builder.append("\nTotal credit :" + format.format(credit));
        builder.append("\nNew Balance :" + format.format(newBal));
        builder.append("\nTotal due :" + format.format(newBal * account.getInterestRate()));


        JTextArea1.setText(builder.toString());
        //}}

        //{{REGISTER_LISTENERS
        SymAction lSymAction = new SymAction();
        JButton_OK.addActionListener(lSymAction);
        //}}
    }


    javax.swing.JScrollPane JScrollPane1 = new javax.swing.JScrollPane();

    javax.swing.JTextArea JTextArea1 = new javax.swing.JTextArea();

    javax.swing.JButton JButton_OK = new javax.swing.JButton();


    class SymAction implements java.awt.event.ActionListener {
        public void actionPerformed(java.awt.event.ActionEvent event) {
            Object object = event.getSource();
            if (object == JButton_OK)
                JButtonOK_actionPerformed(event);
        }
    }

    void JButtonOK_actionPerformed(java.awt.event.ActionEvent event) {
        dispose();
    }
}
